import numpy as np
import cv2 as cv
import json
import os
from matplotlib import pyplot as plt


fig = plt.figure(figsize=(100, 100))        

def draw_img(json_path, root = r'I:\Datasets\手部数据\hand_labels\manual_train'):
    with open(json_path, 'r', encoding='utf8') as f:
        config = json.loads(f.read())

    index = '_0' + str(config['mpii_annorect_idx'] + 1)
    is_left = '_l.' if config['is_left'] == 1 else '_r.' 
    fileName = config['mpii_image'].split('.')[0] + index + is_left + config['mpii_image'].split('.')[1]

    img_path = os.path.join(root, fileName)

    img = cv.imdecode(np.fromfile(img_path, dtype=np.uint8), -1)
    img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
    # img = cv.imread(img_path)
    # cv.imshow('img', img)
    # cv.waitKey(0)

    # plt.subplot(1, 2, 1)
    # plt.title('org_img')
    # plt.imshow(img.astype(np.float32) / 255.0)
    # plt.show()

    bodys = config['mpii_body_pts']
    for points in bodys:
        # print(points)
        cv.circle(img, (int(points[0]), int(points[1])), 3, (0, 0, 255), -1)
        cv.putText(img, str(points[2]), (int(points[0]), int(points[1])), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)

    for points in config['hand_pts']:
        cv.circle(img, (int(points[0]), int(points[1])), 3, (0, 255, 0), -1)
        cv.putText(img, str(points[2]), (int(points[0]), int(points[1])), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)


    # plt.subplot(1, 2, 2)
    plt.title('draw_img')
    plt.imshow(img.astype(np.float32) / 255.0)

    plt.get_current_fig_manager().window.state('zoomed')
    plt.show()


root = r'I:\Datasets\手部数据\hand_labels\manual_train'
for item in os.listdir(root):
    if item.endswith('.json'):
        draw_img(os.path.join(root, item))
        